// pages/index/index.js
Page({
  /**
   * 页面的初始数据
   */
  data: {
    time: '12:30',
    mainFunctions: [
      {
        id: 'dashboard',
        name: '数据看板',
        icon: 'icon-bar-chart-2',
        url: '/pages/dashboard/dashboard'
      },
      {
        id: 'clients',
        name: '客资管理',
        icon: 'icon-users',
        url: '/pages/clients/clients'
      },
      {
        id: 'orders',
        name: '订单管理',
        icon: 'icon-shopping-bag',
        url: '/pages/orders/orders'
      },
      {
        id: 'dresses',
        name: '礼服管理',
        icon: 'icon-package',
        url: '/pages/dresses/dresses'
      },
      {
        id: 'calendar',
        name: '档期管理',
        icon: 'icon-calendar',
        url: '/pages/dress_calendar/dress_calendar'
      },
      {
        id: 'settings',
        name: '系统设置',
        icon: 'icon-settings',
        url: '/pages/settings/settings'
      }
    ],
    announcements: [
      {
        id: 1,
        title: '系统更新通知',
        content: '系统将于今晚22:00-24:00进行例行维护更新，请您提前做好工作安排。',
        date: '2024-03-22'
      },
      {
        id: 2,
        title: '新款礼服入库',
        content: '2024春季新款礼服已入库，请各位顾问及时了解新品详情。',
        date: '2024-03-20'
      }
    ],
    todayEvents: []
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad(options) {
    // 更新当前时间
    this.updateTime();
    this.getTimerID = setInterval(this.updateTime, 60000);
    
    // 获取今日事件
    this.loadTodayEvents();
    
    // 检查登录状态
    const isLoggedIn = wx.getStorageSync('isLoggedIn');
    if (!isLoggedIn) {
      wx.redirectTo({
        url: '/pages/login/login'
      });
    }
  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow() {
    // 每次页面显示时刷新今日事件
    this.loadTodayEvents();
  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload() {
    clearInterval(this.getTimerID);
  },

  updateTime: function() {
    const date = new Date();
    const hours = date.getHours();
    const minutes = date.getMinutes();
    this.setData({
      time: `${hours.toString().padStart(2, '0')}:${minutes.toString().padStart(2, '0')}`
    });
  },

  // 加载今日事件
  loadTodayEvents: function() {
    // 这里应该是API请求，目前使用模拟数据
    const today = new Date();
    const year = today.getFullYear();
    const month = today.getMonth() + 1;
    const day = today.getDate();
    const dateString = `${year}-${month.toString().padStart(2, '0')}-${day.toString().padStart(2, '0')}`;
    
    // 模拟今日事件数据
    const events = [
      {
        id: 1,
        time: '10:00',
        title: '周小姐婚纱取件',
        type: 'pickup',
        typeText: '取件'
      },
      {
        id: 2,
        time: '14:30',
        title: '陈小姐量体服务',
        type: 'measure',
        typeText: '量体'
      },
      {
        id: 3,
        time: '16:00',
        title: '刘小姐试装',
        type: 'try',
        typeText: '试装'
      }
    ];
    
    this.setData({
      todayEvents: events
    });
  },

  // 点击功能模块
  navigateToFunction: function(e) {
    const url = e.currentTarget.dataset.url;
    wx.navigateTo({
      url: url
    });
  },

  // 查看事件详情
  viewEventDetail: function(e) {
    const eventId = e.currentTarget.dataset.id;
    wx.navigateTo({
      url: `/pages/event_detail/event_detail?id=${eventId}`
    });
  },

  // 查看所有通知
  viewAllAnnouncements: function() {
    wx.navigateTo({
      url: '/pages/announcements/announcements'
    });
  },
  
  // 退出登录
  logout: function() {
    wx.showModal({
      title: '退出登录',
      content: '确定要退出登录吗？',
      confirmColor: '#B76E79',
      success: (res) => {
        if (res.confirm) {
          // 清除登录状态
          wx.removeStorageSync('isLoggedIn');
          
          // 跳转到登录页
          wx.reLaunch({
            url: '/pages/login/login'
          });
        }
      }
    });
  }
})
